﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace ScoutNET.Attributes
{
    public class NotAuthorizeLoggedAttribute :  AuthorizeAttribute
    {
        #region Methodes
        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
            if (httpContext == null) throw new ArgumentNullException("httpContext");

            if (httpContext.User.Identity.IsAuthenticated == false)
                return true;
            else
                return false;
        }

        public override void OnAuthorization(AuthorizationContext filterContext)
        {
            base.OnAuthorization(filterContext);
            if (filterContext.Result is HttpUnauthorizedResult)
            {
                filterContext.Result = new RedirectToRouteResult(
                    new RouteValueDictionary
                    {
                        { "client", filterContext.RouteData.Values[ "client" ] },
                        { "controller", "Home" },
                        { "action", "Index" }
                    });
            }
        }
        #endregion
    }
}
